package service.impl;

import dao.LendDao;
import dao.UserDao;
import dao.impl.LendDaoImpl;
import dao.impl.UserDaoImpl;
import entity.User;
import service.UserService;
import entity.Record;
import util.Util;

import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao userDao=new UserDaoImpl();
    LendDao lendDao=new LendDaoImpl();

    public User queryStudentById(String userId) {
        try {
            if(userDao.isExist(userId)){
                User user=userDao.queryStudentById(userId);
                return user;
            }else{
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<User> queryAllStudents() {
        try {
            List<User> users=userDao.queryAllUsers();
            return users;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public List<User> queryAllUsersByRequire(String sql,Object[] params){
        try {
            List<User> users=userDao.queryAllUsersByRequire(sql,params);
            return users;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public boolean updateUserById(String useId, User user) {
        try {
            if(userDao.isExist(useId)){
                if(userDao.updateUserById(useId,user)){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteUserById(String userId) {
        try {
            if(userDao.isExist(userId)){
                List<Record> records=lendDao.borrowInformation(userId);
                if(records!=null){
                    for(Record record:records){
                        if(lendDao.userIsNotReturn(record.getUserId(),record.getBookId())){
                            return false;
                        }
                    }
                }
                if(userDao.deleteUserById(userId)){
                    return true;
                   }else{
                    return false;
                }
            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean addUser(User user) {
        try {
            if(userDao.isExist(user.getUserId())){
                return false;
            }else{
                if(userDao.addUser(user)){
                    return true;
                }else{
                    return false;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public int getUserTatalCount(String sql,Object[] params) {
        try {
            int count=userDao.getTotalCount(sql,params);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }

    public List<User> queryStudentByPage(int currentPage, int pageSize) {
        try {
            List<User> users=userDao.queryUserByPage(currentPage,pageSize);
            return users;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //根据账号或姓名查询当前页的数据集合 currentPage(当前页码) pageSize(页面大小)
    public List<User> queryUserByPageRequire(int currentPage,int pageSize,Object param){
        try {
            List<User> users=userDao.queryUserByPageRequire(currentPage,pageSize,param);
            return users;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

//    public static void main(String[] args) {
//        System.out.println(new UserServiceImpl().deleteUserById("admin_1"));
//        User user=new User("manager","admin_13","2002-05","安徽省蚌埠市","男","admin","管理员", Util.getDate());
//        System.out.println(new UserServiceImpl().addUser(user));
//        System.out.println(new UserServiceImpl().getUserTatalCount("select count(*) from users",null));
//        System.out.println(new UserServiceImpl().queryStudentById("admin").getUserId());
//        User user=new User("manager","admin_13","2002-05","安徽省蚌埠市","Boy","admin","管理员", Util.getDate());
//        System.out.println(new UserServiceImpl().updateUserById("admin_13",user));
//        List<User> users=new UserServiceImpl().queryAllStudents();
//        for(User user:users){
//            System.out.println(user.getUserId());
//        }
//        List<User> users=new UserServiceImpl().queryStudentByPage(0,2);
//        for(User user:users){
//            System.out.println(user.getUserId());
//        }
//    }
}
